﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
using Oracle.ManagedDataAccess.Client;
using System.Data;

namespace GPS.Controllers.GPS_NghiepVu
{
    public class GPS_DiemHocVienSatHachController : Controller
    {
        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;

        #region Index
        public ActionResult Index(ViewModelDDTSH_KQSH SearchModel, int? currentPage)
        {
            initialCategorySearchAction(SearchModel);
            var entities = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GETDS_KQSH_HOCVIEN(SearchModel.pDVGTVT_MA
                                                                                               , SearchModel.pKH_MA
                                                                                               , SearchModel.pKH_LOP_MA
                                                                                               , SearchModel.pHV_MA
                                                                                               , SearchModel.pHV_HOTEN
                                                                                               , SearchModel.pHV_CMND
                                                                                               , SearchModel.pHV_SBD
                                                                                              );

            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = entities.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);

            ViewBag.TotalRow = entities.Count();

            return View(SearchModel);
        }
        public void initialCategorySearchAction(ViewModelDDTSH_KQSH viewModel)
        {
            ViewBag.DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");

        }
        #endregion

        #region Create
        public ActionResult Create(string id1, string id2, string id3, string id4)
        {
            string HV_MA = id2;
            string HV_DVGTVT = id1;
            DHV _hocvien =  _db.GetOne<DHV>(o=>o.HV_MA==HV_MA && o.HV_DVGTVT_MA==HV_DVGTVT);
            CrudModelDDTSH_KQSH _model = new CrudModelDDTSH_KQSH();

            ViewBag.MA_HV = _hocvien.HV_MA;
           // ViewBag.MA_LOP = _hocvien.HV_KH_LOP_MA;
            ViewBag.MA_KH = _hocvien.HV_KH_MA;
            ViewBag.TEN_HV = _hocvien.HV_HODEM + " " + _hocvien.HV_TEN;

            _model.DTSH_KQSH_DVGTVT_MA = _hocvien.HV_DVGTVT_MA;
            _model.DTSH_KQSH_DTSH_TTSH_MA = id3;
            _model.DTSH_KQSH_SBD = id4;

            ViewBag.SSHVs = new SelectList(_db.GetAll<SSHV>(), "SHV_MA", "SHV_HOTEN");

            return PartialView("_Create", _model);
        }

        [HttpPost]
        public ActionResult Create(CrudModelDDTSH_KQSH model, FormCollection form)
        {
            if (ModelState.IsValid)
            {
                DDTSH_KQSH _model = new DDTSH_KQSH();
                GPS.COMMON.Helpers.CopyObject<DDTSH_KQSH>(model, ref _model);
              //  _model.DTSH_KQSH_HV_MA = form["MA_HV"].ToString(); 
                _model.DTSH_KQSH_NGAYNHAP = DateTime.Now;
                _model.DTSH_KQSH_NGUOINHAP = User.Identity.Name;
                _model.ID = EFSequence.GetNextValue("SEQ_DDTSH_KQSH_ID");
                _db.Insert<DDTSH_KQSH>(_model);

                //Begin transaction
                //   _db.BeginTransaction();

                //update DHV_HS:
                // var _person = new DHV_HS() { HV_HS_HV_MA = model.DTTN_KQTN_HV_MA, HV_HS_DVGTVT_MA = model.DTTN_KQTN_DVGTVT_MA, HV_HS_XEPLOAITN = model.DTTN_KQTN_XH };
                var _hv_ma = form["MA_HV"].ToString();
                var _DHV_HS = _db.GetOne<DHV_HS>(o => o.HV_HS_DVGTVT_MA == model.DTSH_KQSH_DVGTVT_MA && o.HV_HS_HV_MA == _hv_ma);
                if (_DHV_HS !=null)
                {
                    _DHV_HS.HV_HS_KQSH = model.DTSH_KQSH_KQ;
                    _DHV_HS.HV_HS_KQDUONG = model.DTSH_KQSH_TD;
                    _DHV_HS.HV_HS_NXDUONG = model.DTSH_KQSH_NXSHV_TD;
                    _DHV_HS.HV_HS_SOBAODANH = model.DTSH_KQSH_SBD;

                    _DHV_HS.HV_HS_KQHINH = model.DTSH_KQSH_TH;
                    _DHV_HS.HV_HS_NXHINH = model.DTSH_KQSH_NXSHV_TH;
                    _DHV_HS.HV_HS_KQLT = model.DTSH_KQSH_LT;
                    _DHV_HS.HV_HS_NXLT = model.DTSH_KQSH_NXSHV_LT;
                    _DHV_HS.HV_HS_KQSH = model.DTSH_KQSH_KQ;

                    _db.Update<DHV_HS>(_DHV_HS);
                }
               
                //_db.DbContext.DHV_HS.Attach(_person);
                //_db.DbContext.Entry(_person).Property(X => X.HV_HS_XEPLOAITN).IsModified = true;
                //_db.DbContext.SaveChanges();

                //_db.CommitTransaction();
                //Commit transaction

                //return RedirectToAction("Index");
                string url = Url.Action("Index", "GPS_DiemHocVienSatHach");

                return Json(new { success = true, url = url });
            }

            return View(model);
        }

        #endregion

        #region Edit
        public ActionResult Edit(string id1, string id2, decimal id)
        {

            DDTSH_KQSH model = _db.GetOne<DDTSH_KQSH>(o => o.ID==id); //await _db.GetDbSet<DDTSH_KQSH>().FindAsync(id1, id3, id4);
            CrudModelDDTSH_KQSH _model = new CrudModelDDTSH_KQSH();
            GPS.COMMON.Helpers.CopyObject<CrudModelDDTSH_KQSH>(model, ref _model);
            ViewBag.SSHVs = new SelectList(_db.GetAll<SSHV>(), "SHV_MA", "SHV_HOTEN");
            string HV_MA = id2;
            string HV_DVGTVT = id1;
            DHV _hocvien = _db.GetOne<DHV>(o => o.HV_MA == HV_MA && o.HV_DVGTVT_MA == HV_DVGTVT);
            ViewBag.MA_HV = _hocvien.HV_MA;
           // ViewBag.MA_LOP = _hocvien.HV_KH_LOP_MA;
            ViewBag.MA_KH = _hocvien.HV_KH_MA;
            ViewBag.TEN_HV = _hocvien.HV_HODEM + " " + _hocvien.HV_TEN;

            return PartialView("_Edit", _model);
        }

        [HttpPost]
        public ActionResult Edit(CrudModelDDTSH_KQSH model, FormCollection form)
        {
            if (ModelState.IsValid)
            {
                var _model = _db.GetOne<DDTSH_KQSH>(o => o.DTSH_KQSH_DVGTVT_MA == model.DTSH_KQSH_DVGTVT_MA && o.DTSH_KQSH_DVGTVT_MA == model.DTSH_KQSH_DTSH_TTSH_MA && o.DTSH_KQSH_SBD == model.DTSH_KQSH_SBD);

                GPS.COMMON.Helpers.CopyObject<DDTSH_KQSH>(model, ref _model, new List<string> { "DTSH_KQSH_NGAYNHAP", "DTSH_KQSH_NGUOINHAP" });
                _model.DTSH_KQSH_NGAYSUA = DateTime.Now;
                _model.DTSH_KQSH_NGUOISUA = User.Identity.Name;

                _db.Update<DDTSH_KQSH>(_model);
                //update DHV_HS:
                // var _person = new DHV_HS() { HV_HS_HV_MA = model.DTTN_KQTN_HV_MA, HV_HS_DVGTVT_MA = model.DTTN_KQTN_DVGTVT_MA, HV_HS_XEPLOAITN = model.DTTN_KQTN_XH };
                var _hv_ma = form["MA_HV"].ToString();
                var _DHV_HS = _db.GetOne<DHV_HS>(o => o.HV_HS_DVGTVT_MA == model.DTSH_KQSH_DVGTVT_MA && o.HV_HS_HV_MA == _hv_ma);
                if (_DHV_HS!=null)
                {
                    _DHV_HS.HV_HS_KQSH = model.DTSH_KQSH_KQ;
                    _DHV_HS.HV_HS_KQDUONG = model.DTSH_KQSH_TD;
                    _DHV_HS.HV_HS_NXDUONG = model.DTSH_KQSH_NXSHV_TD;
                    _DHV_HS.HV_HS_SOBAODANH = model.DTSH_KQSH_SBD;

                    _DHV_HS.HV_HS_KQHINH = model.DTSH_KQSH_TH;
                    _DHV_HS.HV_HS_NXHINH = model.DTSH_KQSH_NXSHV_TH;
                    _DHV_HS.HV_HS_KQLT = model.DTSH_KQSH_LT;
                    _DHV_HS.HV_HS_NXLT = model.DTSH_KQSH_NXSHV_LT;
                    _DHV_HS.HV_HS_KQSH = model.DTSH_KQSH_KQ;

                    _db.Update<DHV_HS>(_DHV_HS);
                }
                
                //_db.DbContext.DHV_HS.Attach(_person);
                //_db.DbContext.Entry(_person).Property(X => X.HV_HS_XEPLOAITN).IsModified = true;
                //_db.DbContext.SaveChanges();

                //_db.CommitTransaction();
                //Commit transaction
                //return RedirectToAction("Index");
                string url = Url.Action("Index", "GPS_DiemHocVienSatHach");

                return Json(new { success = true, url = url });
            }

            return View(model);
        }


        #endregion

        #region Delete
        public ActionResult Delete(decimal id,string id4)
        {

            DDTSH_KQSH model = _db.GetOne<DDTSH_KQSH>(o => o.ID==id);
            _db.DeleteItem<DDTSH_KQSH>(model);

            //update DHV_HS:
            // var _person = new DHV_HS() { HV_HS_HV_MA = model.DTTN_KQTN_HV_MA, HV_HS_DVGTVT_MA = model.DTTN_KQTN_DVGTVT_MA, HV_HS_XEPLOAITN = model.DTTN_KQTN_XH };
            var _hv_ma = id4;
            var _DHV_HS = _db.GetOne<DHV_HS>(o => o.HV_HS_DVGTVT_MA == model.DTSH_KQSH_DVGTVT_MA && o.HV_HS_HV_MA == _hv_ma);
            if (_DHV_HS !=null)
            {
                _DHV_HS.HV_HS_KQSH = null;
                _DHV_HS.HV_HS_KQDUONG = null;
                _DHV_HS.HV_HS_NXDUONG = null;
                // _DHV_HS.HV_HS_SOBAODANH = model.DTSH_KQSH_SBD;

                _DHV_HS.HV_HS_KQHINH = null;
                _DHV_HS.HV_HS_NXHINH = null;
                _DHV_HS.HV_HS_KQLT = null;
                _DHV_HS.HV_HS_NXLT = null;
                _DHV_HS.HV_HS_KQSH = null;

                _db.Update<DHV_HS>(_DHV_HS);
            }
            

            return RedirectToAction("Index");
        }
        #endregion

        #region detail
        public async Task<ActionResult> Detail(string id1,decimal id   , string id4)
        {
            DDTSH_KQSH model = await _db.GetDbSet<DDTSH_KQSH>().FindAsync(id);
            CrudModelDDTSH_KQSH _model = new CrudModelDDTSH_KQSH();
            GPS.COMMON.Helpers.CopyObject<CrudModelDDTSH_KQSH>(model, ref _model);
            ViewBag.SSHVs = new SelectList(_db.GetAll<SSHV>(), "SHV_MA", "SHV_HOTEN");
            string HV_MA = id4;
            string HV_DVGTVT = id1;
            DHV _hocvien = _db.GetOne<DHV>(o => o.HV_MA == HV_MA && o.HV_DVGTVT_MA == HV_DVGTVT);
            ViewBag.TEN_HV = _hocvien.HV_HODEM + " " + _hocvien.HV_TEN;

            return PartialView("_Detail", _model);
        }

        #endregion

        #region "function"

        #endregion
    }
}